package org.csu.mypetstore.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.entity.*;
import org.csu.mypetstore.api.persistence.CategoryMapper;
import org.csu.mypetstore.api.persistence.ItemInventoryMapper;
import org.csu.mypetstore.api.persistence.ItemMapper;
import org.csu.mypetstore.api.persistence.ProductMapper;
import org.csu.mypetstore.api.service.CatalogService;
import org.csu.mypetstore.api.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service("catalogService")
public class CatalogServiceImpl implements CatalogService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemInventoryMapper itemInventoryMapper;


    @Override
    //获取全部category
    public CommonResponse<List<Category>> getCategoryList(){
        List<Category> categoryList = categoryMapper.selectList(null);
        if(categoryList.isEmpty())
            return CommonResponse.createForError("没有任何类");
        else
            return CommonResponse.createForSuccess(categoryList);
    }

    @Override
    //根据Id获取category
    public CommonResponse<Category> getCategoryById(String categoryId){
        Category category = categoryMapper.selectById(categoryId);
        if(category == null)
            return CommonResponse.createForError("没有该ID的category");
        else
            return CommonResponse.createForSuccess(category);
    }

    @Override
    //根据Id更新category
    public CommonResponse<Category> updateCategoryById(Category category){
        UpdateWrapper<Category> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("catid",category.getCategoryId());
        updateWrapper.set("name",category.getName());
        updateWrapper.set("descn",category.getDescription());

        if(categoryMapper.update(null,updateWrapper) == 1)
            return CommonResponse.createForSuccessMessage("修改成功");
        else
            return CommonResponse.createForError("修改失败");
    }

    @Override
    //增加category(未完成)
    public CommonResponse<Category> insertCategory(String categoryId,String name,String description){
        Category category = new Category();
        category.setCategoryId(categoryId);
        category.setName(name);
        category.setDescription(description);

        if(categoryMapper.insert(category) == 1)
            return CommonResponse.createForSuccessMessage("添加成功");
        else
            return CommonResponse.createForError("添加失败");
    }

    @Override
    //根据categoryId获取products
    public CommonResponse<List<Product>> getProductByCatid(@PathVariable("id") String categoryId){
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",categoryId);
        List<Product> productList = productMapper.selectList(queryWrapper);

        if(productList.isEmpty())
            return CommonResponse.createForError("该Category下无Product");
        else
            return CommonResponse.createForSuccess(productList);
    }

    @Override
    //根据productId获取product
    public CommonResponse<Product> getProductByProid(@PathVariable("id") String productId) {
        Product product = productMapper.selectById(productId);

        if(product == null)
            return CommonResponse.createForError("没有该id的Product");
        else
            return CommonResponse.createForSuccess(product);
    }

    @Override
    //根据模糊查询获取product
    public CommonResponse<List<Product>> getProductLikeProid(String productId){
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("productid",productId);
        List<Product> productList = productMapper.selectList(queryWrapper);
        if(productList.isEmpty())
            return CommonResponse.createForError("没有查询到相关结果");
        else
            return CommonResponse.createForSuccess(productList);
    }


    @Override
    //根据productId获取items
    public CommonResponse<List<Item>> getItemByProid(@PathVariable("id") String productId){
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid",productId);
        List<Item> itemList = itemMapper.selectList(queryWrapper);

        if(itemList.isEmpty())
            return CommonResponse.createForError("该Product下无item");
        else
            return CommonResponse.createForSuccess(itemList);
    }

    @Override
    //根据itemId获取item
    public CommonResponse<Item> getItemByItemid(String itemId) {
        Item item = itemMapper.selectById(itemId);

        if(item == null)
            return CommonResponse.createForError("没有该id的Item");
        else
            return CommonResponse.createForSuccess(item);
    }

    @Override
    public CommonResponse<List<ItemVO>> getItemVOByProductId(String productId){
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid",productId);
        List<Item> itemList = itemMapper.selectList(queryWrapper);

        if(itemList.isEmpty())
            return CommonResponse.createForError("该product下没有item商品");

        Product product = productMapper.selectById(productId);
        List<ItemVO> itemVOList = new ArrayList<>();
        for(Item item : itemList){
            ItemVO itemVO = itemToItemVO(item,product);
            itemVOList.add(itemVO);
        }
        return CommonResponse.createForSuccess(itemVOList);
    }

    private ItemVO itemToItemVO(Item item, Product product){
        ItemVO itemVO = new ItemVO();

        itemVO.setItemId(item.getItemId());
        itemVO.setProductId(item.getProductId());
        itemVO.setListPrice(item.getListPrice());
        itemVO.setUnitCost(item.getUnitCost());
        itemVO.setSupplierId(item.getSupplierId());
        itemVO.setSupplierId(item.getSupplierId());
        itemVO.setStatus(item.getStatus());
        itemVO.setAttribute1(item.getAttribute1());
        itemVO.setAttribute2(item.getAttribute2());
        itemVO.setAttribute3(item.getAttribute3());
        itemVO.setAttribute4(item.getAttribute4());
        itemVO.setAttribute5(item.getAttribute5());

        itemVO.setCategoryId(product.getCategoryId());
        itemVO.setProductName(product.getName());
        itemVO.setProductDescription(product.getDescription());

        ItemInventory itemInventory = itemInventoryMapper.selectById(item.getItemId());

        return itemVO;
    }
}
